package com.iteaj.iot.client.component;

import com.iteaj.iot.ConcurrentStorageManager;
import com.iteaj.iot.client.ClientComponent;
import com.iteaj.iot.client.IotClient;
import com.iteaj.iot.client.MultiClientManager;
import com.iteaj.iot.config.ConnectProperties;

import java.util.Collection;

/**
 * 简单的客户端管理
 */
public class SimpleMultiClientManager extends ConcurrentStorageManager<String, IotClient> implements MultiClientManager {

    private ClientComponent clientComponent;

    public SimpleMultiClientManager(ClientComponent clientComponent) {
        this.clientComponent = clientComponent;
    }

    @Override
    public void addClient(Object clientKey, IotClient value) {
        String clientKeyStr = null;
        if(clientKey instanceof ConnectProperties) {
            clientKeyStr = ((ConnectProperties) clientKey).getHost()+":" + ((ConnectProperties) clientKey).getPort();
        }

        add(clientKeyStr, value);
    }

    @Override
    public IotClient getClient(Object clientKey) {
        String clientKeyStr = null;
        if(clientKey instanceof ConnectProperties) {
            clientKeyStr = ((ConnectProperties) clientKey).getHost()+":" + ((ConnectProperties) clientKey).getPort();
        }

        return get(clientKeyStr);
    }

    @Override
    public IotClient removeClient(Object clientKey) {
        String clientKeyStr = null;
        if(clientKey instanceof ConnectProperties) {
            clientKeyStr = ((ConnectProperties) clientKey).getHost()+":" + ((ConnectProperties) clientKey).getPort();
        }

        return remove(clientKeyStr);
    }

    @Override
    public Collection<IotClient> clients() {
        return getMapper().values();
    }

    @Override
    public ClientComponent getClientComponent() {
        return this.clientComponent;
    }

    @Override
    public void setClientComponent(ClientComponent clientComponent) {
        this.clientComponent = clientComponent;
    }

}
